//
//  ByWithdrawDetailVC.m
//  caipiao_ios
//
//  Created by mc on 2018/8/9.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByWithdrawDetailVC.h"
#import "ByApplyTitleTFView.h"


@interface ByWithdrawDetailVC ()<UITextFieldDelegate>

@property (nonatomic, strong) ByApplyTitleTFView *TF1;
@property (nonatomic, strong) ByApplyTitleTFView *TF2;
@property (nonatomic, strong) ByApplyTitleTFView *TF3;

@property (nonatomic, strong) UIButton *button;

@property (nonatomic, strong) UITextField *textField1;



@end

@implementation ByWithdrawDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"提现";
    [self addLeftBackBarButtonItem];
    
    [self initViews];
    // Do any additional setup after loading the view.
}


- (void)initViews{
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
   
    ByApplyTitleTFView *TF1 = [[ByApplyTitleTFView alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, 49)];
    [self.view addSubview:TF1];
    self.TF1 = TF1;
   
    
    ByApplyTitleTFView *TF2 = [[ByApplyTitleTFView alloc] initWithFrame:CGRectMake(0, 1 + TF1.bottom, SCREEN_WIDTH, 49)];
    [self.view addSubview:TF2];
    self.TF2 = TF2;
  
    CGFloat h = 10;
    if (self.type == 1) {
        ByApplyTitleTFView *TF3 = [[ByApplyTitleTFView alloc] initWithFrame:CGRectMake(0, 1 + TF2.bottom, SCREEN_WIDTH, 49)];
        [self.view addSubview:TF3];
        h = 49 + h;
        TF1.textField.placeholder = @"银行卡账号";
        TF2.textField.placeholder = @"姓名";
        TF3.textField.placeholder = @"开户行地址";
        TF2.titleLabel.text = @"电话";
        TF3.titleLabel.text = @"开户行";
        self.TF3 = TF3;
        
    }else{
        
        TF1.textField.placeholder = @"支付宝账号";
        TF2.titleLabel.text = @"姓名";
        TF2.textField.placeholder = @"姓名";
    }
    
    UILabel *noteLabel = [[UILabel alloc] init];
    [self.view addSubview:noteLabel];
    [noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(17);
        make.top.equalTo(TF2.mas_bottom).offset(h);
        make.right.equalTo(self.view).offset(-17);
        make.height.mas_equalTo(@36);
    }];
    noteLabel.font = FONT(11);
    noteLabel.textColor = [UIColor lightGrayColor];
    noteLabel.text = @"账号信息与实名认证一致，否则无法提现";
    noteLabel.numberOfLines = 0;
    
    
    UIView *moneyViw = [[UIView alloc] init];
    [self.view addSubview:moneyViw];
    [moneyViw mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.top.equalTo(noteLabel.mas_bottom).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.height.mas_equalTo(@130);
    }];
    
    moneyViw.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel1 = [[UILabel alloc] init];
    [moneyViw addSubview:titleLabel1];
    [titleLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(moneyViw).offset(10);
        make.top.equalTo(moneyViw).offset(13);
        make.size.mas_equalTo(CGSizeMake(180, 15));
    }];
    
    titleLabel1.font = FONT(14);
    titleLabel1.textColor = BY_TEXT_COLOR;
    titleLabel1.text = @"提现金额(元)";
    titleLabel1.textAlignment = NSTextAlignmentLeft;
    titleLabel1.backgroundColor = [UIColor whiteColor];
    
    
    UITextField *textField1 = [[UITextField alloc] init];
    [moneyViw addSubview:textField1];
    [textField1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel1.mas_bottom).offset(22);
        make.left.equalTo(moneyViw).offset(10);
        make.right.equalTo(self.view).offset(-10);
        make.height.mas_equalTo(@40);
    }];
    textField1.placeholder = @"提现金额（元）";
    textField1.font =B_FONT(30);
    textField1.backgroundColor = [UIColor whiteColor];
    textField1.textAlignment = NSTextAlignmentLeft;
    textField1.textColor = BY_THEME_COLOR;
    textField1.keyboardType = UIKeyboardTypeNumberPad;
    textField1.tag = 200;
    textField1.delegate = self;
    self.textField1 = textField1;
    
    UILabel *line = [[UILabel alloc] init];
    [moneyViw addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(moneyViw).offset(6);
        make.right.equalTo(moneyViw).offset(-6);
        make.bottom.equalTo(moneyViw).offset(-37);
        make.height.mas_equalTo(@2);
    }];
    line.backgroundColor = BY_BACKGROUND_COLOR;
    
    
    UIImageView *iconView = [[UIImageView alloc] init];
    [moneyViw addSubview:iconView];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(moneyViw).offset(10);
        make.bottom.equalTo(moneyViw).offset(-13);
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];
    iconView.image = [UIImage imageNamed:@"tixianjinet提现金额"];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [moneyViw addSubview:rightBtn];
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(moneyViw).offset(-9);
        make.bottom.equalTo(moneyViw).offset(-12);
        make.size.mas_equalTo(CGSizeMake(60, 15));
    }];
    rightBtn.titleLabel.font = FONT(14);
    NSMutableAttributedString *attributedString= [[NSMutableAttributedString alloc] initWithString:@"全部提现"];
    [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, 4)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:BY_THEME_COLOR range:NSMakeRange(0, 4)];
    [rightBtn setAttributedTitle:attributedString
                        forState:UIControlStateNormal];
    
    [rightBtn addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel *valueLabel = [[UILabel alloc] init];
    [moneyViw addSubview:valueLabel];
    [valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconView.mas_right).offset(3);
        make.right.equalTo(rightBtn.mas_left).offset(-20);
        make.bottom.equalTo(moneyViw).offset(-13);
        make.height.mas_equalTo(@16);
    }];
    valueLabel.textColor = [UIColor blackColor];
    valueLabel.textAlignment = NSTextAlignmentLeft;
    valueLabel.font = FONT(12);
    valueLabel.text = [NSString stringWithFormat:@"可提现金额:%@", BY_USER.withdrawals_money];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(52);
        make.right.equalTo(self.view).offset(-52);
        make.top.equalTo(moneyViw.mas_bottom).offset(50);
        make.height.mas_equalTo(@50);
    }];
    button.backgroundColor = rgba(137, 137, 137, 1);
    [button setTitle:@"立即提现" forState:UIControlStateNormal];
    button.titleLabel.font = FONT(16);
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    button.userInteractionEnabled = NO;
    self.button = button;
    
}

- (void)btnAction:(UIButton *)btn{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    
    if (self.TF1.textField.text.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请输入提现账号"];
        return;
    }
    
    if (self.TF2.textField.text.length == 0){
        [ByCoreToolAlert showOnlyText:@"请输入姓名"];
        return;
    }
    
    if (self.textField1.text.integerValue > BY_USER.withdrawals_money.integerValue) {
        [ByCoreToolAlert showOnlyText:@"提现金额不能大于余额"];
        return;
    }
    
    [ByCoreToolAlert showLoadind:@"提交中" hasMasker:YES];
    ByNetModel *netModel = [[ByNetModel alloc] init];
    netModel.money = self.textField1.text;
    netModel.account = self.TF1.textField.text;
    netModel.owner_name = self.TF2.textField.text;
    if (self.type == 0) {
        netModel.account_type = @"alipay";
    }else if (self.type == 1){
        netModel.account_type = @"bank";
        netModel.bank_branch_address = self.TF3.textField.text;
    }else if (self.type == 2){
         netModel.account_type = @"weixin";
    }

    [ByMoneyApi userMoneyApply:netModel success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showSuccessWithText:@"提现成功"];
        ByModelUser *user = BY_USER;
        user.withdrawals_money = [NSString stringWithFormat:@"%ld", user.withdrawals_money.integerValue - netModel.money.integerValue];
        [ByCacheUser setUserInfo:user];
        [self leftItemsAction:0];
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
    
}

- (void)rightAction:(UIButton *)btn{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    self.textField1.text = [NSString stringWithFormat:@"%@",BY_USER.withdrawals_money];
    if (self.TF1.textField.text.length > 0 && self.textField1.text.length > 0 && self.TF2.textField.text.length > 0) {
        if (self.type == 1 ){
            if (self.TF2.textField.text.length > 0) {
                self.button.userInteractionEnabled = YES;
                self.button.backgroundColor = BY_THEME_COLOR;
            }else{
                self.button.userInteractionEnabled = NO;
                self.button.backgroundColor = rgba(137, 137, 137, 1);
            }
        }else{
            self.button.userInteractionEnabled = YES;
            self.button.backgroundColor = BY_THEME_COLOR;
        }
       
    }else{
        self.button.userInteractionEnabled = NO;
        self.button.backgroundColor = rgba(137, 137, 137, 1);
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    if (self.TF1.textField.text.length > 0 && self.textField1.text.length > 0 && self.TF2.textField.text.length > 0) {
        if (self.type == 1 ){
            if (self.TF2.textField.text.length > 0) {
                self.button.userInteractionEnabled = YES;
                self.button.backgroundColor = BY_THEME_COLOR;
            }else{
                self.button.userInteractionEnabled = NO;
                self.button.backgroundColor = rgba(137, 137, 137, 1);
            }
        }else{
            self.button.userInteractionEnabled = YES;
            self.button.backgroundColor = BY_THEME_COLOR;
        }
        
    }else{
        self.button.userInteractionEnabled = NO;
        self.button.backgroundColor = rgba(137, 137, 137, 1);
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
